Attach Packages

library(tidyverse)
library(janitor)
library(here)
library(lubridate)
library(paletteer)
library(tsibble)
library(fabletools)
library(fable)
library(feasts)
library(forecast)
library(sf)
library(tmap)
library(mapview)
us_renew <- read_csv("renewables_cons_prod.csv") %>%  
  clean_names()

Clean up data

  • Convert description to all lowercase
  • Only keep observations for “consumption”
  • Remove any “total” observations
renew_clean <- us_renew %>% 
  mutate(description = str_to_lower(description)) %>% 
  filter(str_detect(description, pattern = "consumption")) %>% 
  filter(!str_detect(description, pattern = "total"))

Convert yyyymm column to date with lubridate

renew_date <- renew_clean %>% 
  mutate(yr_mo_day = lubridate::parse_date_time(yyyymm, "ym")) %>% 
  mutate(month_sep = yearmonth(yr_mo_day)) %>% #coerce to tsibble `yearmonth` format
  mutate(value = as.numeric(value)) %>% 
  drop_na(month_sep, value)

## Make a version where i have month and year in separate columns

renew_parsed <- renew_date %>% 
  mutate(month= month(yr_mo_day , label = TRUE)) %>% 
  mutate(year = year(yr_mo_day))

Look at it:

renew_gg <- ggplot(data = renew_date, aes(x=month_sep,
                                          y=value,
                                          group = description)) +
  geom_line(aes(color = description))

renew_gg

Updating colors with paletteer palettes:

renew_gg +
  scale_color_paletteer_d("calecopal::figmtn")

Coerce our renew_parsed to tsibble for timeseries dataframe

renew_ts <- as_tsibble(renew_parsed, key = description, index = month_sep)

Lets look at our ts data in a couple ways

renew_ts %>% autoplot(value)

renew_ts %>% gg_subseries(value)

#renew_ts %>% gg_season(value) --- doesnt work so lets use GGPLOT

ggplot

ggplot(data= renew_parsed, aes(x= month, y= value, group = year)) +
  geom_line(aes(color = year)) +
  facet_wrap(~description,
             ncol = 1,
             scales = "free",
             strip.position = "right")

Just look at the hyroelectic energy consumption

hydro_ts <- renew_ts %>% 
  filter(description == "hydroelectric power consumption")

hydro_ts %>%  autoplot(value)

hydro_ts %>% gg_subseries(value)

ggplot(hydro_ts, aes(x = month, y= value, group = year)) +
  geom_line(aes(color = year))

what if i want the quarterly consumption of hydro - index by is for tsibble for timeseries

hydro_quarterly <- hydro_ts %>% 
  index_by(year_qu = ~ yearquarter(.)) %>% # monthly aggregates the period says based on groups that exist
  summarise(
    avg_consumption = mean(value)
  )

head(hydro_quarterly)
## # A tsibble: 6 x 2 [1Q]
##   year_qu avg_consumption
##     <qtr>           <dbl>
## 1 1973 Q1            261.
## 2 1973 Q2            255.
## 3 1973 Q3            212.
## 4 1973 Q4            225.
## 5 1974 Q1            292.
## 6 1974 Q2            290.

Decompose that hydro_ts

dcmp <- hydro_ts %>%
  model(STL(value ~ season(window = 5)))
        

# View the components
# components(dcmp)

# Visualize the decomposed components
components(dcmp) %>% autoplot() +
  theme_minimal()

# Let's check out the residuals:
hist(components(dcmp)$remainder)

Now look at ACF - auto correlation function time t compared to lags

hydro_ts %>% 
  ACF(value) %>% 
  autoplot()

DANGER OF MODELING NOT PERFECT

hydro_model <- hydro_ts %>%
  model(
    arima = ARIMA(value),
    ets = ETS(value)
  ) %>%
  fabletools::forecast(h = "2 years")

hydro_model %>% 
  autoplot(filter(hydro_ts, 
                  year(month_sep) > 2010), 
           level = NULL)